package com.pinkpig.excel.ext.style;

import com.alibaba.excel.metadata.CellData;
import com.alibaba.excel.metadata.Head;
import com.alibaba.excel.write.metadata.holder.WriteSheetHolder;
import com.alibaba.excel.write.metadata.holder.WriteTableHolder;
import com.alibaba.excel.write.metadata.style.WriteCellStyle;
import com.alibaba.excel.write.style.HorizontalCellStyleStrategy;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Workbook;

import java.util.List;

/**
 * 可刷新样式的策略
 */
public class HorizontalCellStyleRefreshableStrategy extends HorizontalCellStyleStrategy {

    private Workbook workbook;

    public HorizontalCellStyleRefreshableStrategy(WriteCellStyle headWriteCellStyle,
                                       List<WriteCellStyle> contentWriteCellStyleList) {
        super(headWriteCellStyle, contentWriteCellStyleList);
    }

    public HorizontalCellStyleRefreshableStrategy(WriteCellStyle headWriteCellStyle, WriteCellStyle contentWriteCellStyle) {
        super(headWriteCellStyle, contentWriteCellStyle);
    }

    @Override
    public void afterCellDispose(WriteSheetHolder writeSheetHolder, WriteTableHolder writeTableHolder, List<CellData> cellDataList, Cell cell, Head head, Integer relativeRowIndex, Boolean isHead) {
        if (isHead != null && head != null) {
            if (isHead) {
                this.setHeadCellStyle(cell, head, relativeRowIndex);
            } else {
                this.setContentCellStyle(cell, head, relativeRowIndex);
            }

        }else{
            this.setContentCellStyle(cell, head, relativeRowIndex);
        }
    }

    @Override
    protected void initCellStyle(Workbook workbook) {
        super.initCellStyle(workbook);
        this.workbook = workbook;

    }

    /**
     * 重新刷新下样式
     */
    public void refreshCellStyle(){
        if(null!=workbook){
            initCellStyle(workbook);
        }
    }
}
